/*
 * Customer Action
 */
var CKL     = require("../utils/checklogin"),
Response    = require("../utils/response"),
DaoMysql    = require('../models/dao-mysql').DaoMysql,
CacheRedis  = require('../models/cache-redis').CacheRedis,
Driver      = require('../models/driver').Driver;
Page        = require('../models/page').Page;

module.exports = function(app)
{
	var dao = new DaoMysql(app.envConfig.dbMysql, app.mysqlClient, app.logmessage, new CacheRedis(app.envConfig.storeRedis, app.redisClient, app.logmessage, app.defs.DEFAULT_CACHE_EXPIRE_SECS));
	
	//司机列表get请求，验证是否登录
	app.get(app.envConfig.routes.driver_user_list.url,CKL.logined);
	app.get(app.envConfig.routes.driver_user_list.url,function(req,res){
		driverList(req,res);
	});
	
	//司机列表post请求，验证是否登录
	app.post(app.envConfig.routes.driver_user_list.url,CKL.logined);
	app.post(app.envConfig.routes.driver_user_list.url,function(req,res){
		driverList(req,res);
	});
	
	//司机分页查询方法
	function driverList(req,res){
		
		if(!req.body.pageNo){
			req.body.pageNo=0;
		}
		if(!req.body.pageSize){
			req.body.pageSize=5;
		}
		dao.searchPage(Driver,null,req.body.pageNo+","+req.body.pageSize,function(err,result,total){
			if(err){
				msg(err);
			}
			var page=new Page(req.body.pageNo,req.body.pageSize,result,total);
			Response.render(app,res,app.envConfig.routes.driver_user_list.page,{user:req.session.user,page:page});
		});
	}
	
	
	//审核司机
	app.get(app.envConfig.routes.driver_user_check.url,CKL.logined);
	app.get(app.envConfig.routes.driver_user_check.url,function(req,res){
		if(req.params.id){
				dao.get(Driver,req.params.id,function(err,result){
					if(err || !result){
						 return Response.jsonerror(app,res,{msg:"审核失败"});
					}else{
						result.checkdri = 1;
						dao.update(new Driver(result), function(err,result){
							if(err || !result){
								return Response.jsonerror(app,res,{msg:"审核失败"});
							}else{
								return Response.jsonsuccess(app,res,{msg:"审核成功"});
							}
						}); 
					}
				});
		}else{
			return Response.jsonerror(app,res,{msg:"审核失败,参数错误"});
		}
	});
	
	
	//司机详情
	app.get(app.envConfig.routes.driver_user_detail.url,CKL.logined);
	app.get(app.envConfig.routes.driver_user_detail.url,function(req,res){
		if(req.params.id){
				dao.get(Driver,req.params.id,function(err,result){
					if(err){
						msg(err);
					}
					Response.render(app,res,app.envConfig.routes.driver_user_detail.page,{user:req.session.user,result:result});
				});
		}
	});
	
};